// ==UserScript==
// @name           autohelp
// @namespace	http://userscripts.org/users/21452
// @description	automatically does jobs, boosts, & wars for the mafia wars facebook app. 
// @include	http://www.facebook.com/home.php?filter=app_10979261223&show_hidden=true
// @version	67
// ==/UserScript==
window.setTimeout(function (){
	var d = document.body.insertBefore(document.createElement('div'), document.body.firstChild )
	d.appendChild(document.createElement('span')).innerHTML = '<a href="https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=8335810">Donate now!</a><br />this script deletes the news feed items because it doesnt track which links it has visted between each refresh. it automatically unchecks the boxes when you hit the 25 limit, and checks again every 30 minutes.'
	b = d.appendChild(document.createElement('button'))
	b.type = 'button'
	b.setAttribute('onclick', "tb = document.getElementById('AH_tb').childNodes;for(i=1;i<tb.length;i++)tb[i].childNodes[2].textContent = tb[i].childNodes[1].textContent = tb[i].childNodes[3].textContent = 0;document.getElementById('AH_exp').textContent = 0")
	b.textContent = 'reset all counters'
	t = d.appendChild(document.createElement('table'))
	t.id = 'AH_tb'
	tr = t.appendChild(document.createElement('tr'))
	tr.innerHTML = '<th><a href="http://userscripts.org/scripts/show/57429">autohelp</a> v67</th><th>attempt</th><th>success</th><th>exp</th>'
	exp = makeCheckbox('cuba', true)
	exp = exp.appendChild(document.createElement('td'))
	exp.id = 'AH_exp'
	exp.style.fontWeight = '1200'
	makeCheckbox('moscow', true)
	makeCheckbox('ny', true)
	makeCheckbox('boost', true)
	makeCheckbox('war', false)
	refreshTimer = d.appendChild(document.createElement('input'))
	refreshTimer.value = GM_getValue('refreshDelay', 3200)
	refreshTimer.id = 'AH_refreshDelay'
	refreshTimer.parentNode.appendChild(document.createElement('span')).textContent = '(ms) new content delay.'

	function makeCheckbox(name, defaultval) {
		r = t.appendChild(document.createElement('tr'))
		r.id = name
		label = r.appendChild(document.createElement('label'))
		label.innerHTML = '<input type="checkbox" /> ' + name
		r.appendChild(document.createElement('td')).textContent = GM_getValue(name +'Attempt', 0)
		r.appendChild(document.createElement('td')).textContent = GM_getValue(name +'Success', 0)
		r.appendChild(document.createElement('td')).textContent = GM_getValue(name +'Exp', 0)
		label.getElementsByTagName('input')[0].checked = GM_getValue(name, defaultval)
		return r
	}
	
	window.setInterval(function(){
		document.getElementById('cuba').getElementsByTagName('input')[0] = true
		document.getElementById('ny').getElementsByTagName('input')[0] = true
		}, 1800000 )
	window.setInterval(function(){
		tb = document.getElementById('AH_tb').children
		GM_setValue('exp', parseInt(document.getElementById('AH_exp').textContent ) )
		for( var i = 1; i < tb.length; i++ ) {
			n = tb[i].id
			GM_setValue(n + 'Attempt', parseInt(tb[i].childNodes[1].textContent))
			GM_setValue(n + 'Success', parseInt(tb[i].childNodes[2].textContent))
			GM_setValue(n + 'Exp', parseInt(tb[i].childNodes[3].textContent))
			GM_setValue(n, tb[i].getElementsByTagName('input')[0].checked)
		}
	}, 18000)
	AH_go()
	s = document.getElementById('home_sidebar')
	while(s.childNodes[1])
		s.removeChild(s.childNodes[1])
}, 2000)

function AH_go() {
	window.location = 'javascript:void(' + document.getElementsByClassName('UIIntentionalStream_ShowNewStories_Msg')[0].getAttribute('onclick') + ');'
	v = parseInt(document.getElementById('AH_refreshDelay').value)
	if(!v || v < 1300 || v > 20000)
		 v = 3200
	GM_setValue('refreshDelay', v)
	document.getElementById('AH_refreshDelay').value = v
	window.setTimeout(AH_go, v)
	var p = document.getElementById('home_stream').getElementsByClassName('UIIntentionalStream_Content')[0]
	var link = p.getElementsByTagName('a')
	var c = 0
	tb = document.getElementById('AH_tb').children
	for(i = 1; i < tb.length; i++)
		c += parseInt(tb[i].childNodes[3].textContent)
	document.getElementById('AH_exp').textContent = c
	if(link.length == 0) return
	regxs = [ ['cuba', /requestjobhelpshort_cuba/],
		['moscow', /give_help_mos/],
		['ny', /give_help/],
		['boost', /boost_claim/],
		['war', /story_war_helped|story_war_declared/] ]
	
	function redirHelp(URL, t){
		GM_xmlhttpRequest({ method:"get", url: 'http://mwfb.zynga.com/mwfb' + URL, onload: function(r) {
			GM_xmlhttpRequest({ url:r.responseText.toString().split('top.location.href = "')[1].split('"')[0], method:'get',
				onload: function(resp){
					var s = resp.responseText.toString()
					if (!s) return
					row = document.getElementById(t)
					switch(t){
						case 'cuba': case 'moscow': case 'ny':
							if (q = /<span class="good">(\d+) experience points<\/span>/.exec(s) ) {
								row.childNodes[3].textContent = parseInt(row.childNodes[3].textContent) + parseInt(q[1])
								s = row.childNodes[2].textContent
								s = parseInt(s) + 1
							}
							else if( /Sorry, you can only help 25 friends/.test(s) )
								row.getElementsByTagName('input')[0].checked = false
							break;
						case 'boost':
							if( /You received a/.test(s) )
								row.childNodes[2].textContent = parseInt(row.childNodes[2].textContent) + 1
							break;
						case 'war':
							if( (end = s.lastIndexOf('<span class="sexy_fight">Attack</span>')) != -1 ) {
								start = s.lastIndexOf('href="http://apps.facebook.com/inthemafia/remote/html_server.php?', end)
								end = s.indexOf('"',start + 8)
								url = 'http://apps.facebook.com/inthemafia/index.php?'  + s.substring(start + 65, end)
								url = url.replace(/&amp;/g, '&')
								GM_xmlhttpRequest({ method:'get', url:url, onload:function(r){
									s = r.responseText.toString()
									if(!s) return
									if(/LOST|WON/.test(s)) {
										exp = row.childNodes[3]
										exp.textContent = parseInt(exp.textContent) + 3
										row.childNodes[2].textContent = parseInt(row.childNodes[2].textContent) + 1
									}
								} })
							}
					}
				}
			})
		} })
	}

	visted = {}
		
	for( var i = 0; i < link.length; i++ ){
		if (URL = /(\/track.php\?sendkey=(\w+)&.+)/.exec(link[i].href) ){
			k = URL[2]
			if(visted[k]) continue
			for(j = 0; j < regxs.length; j++) {
				if (regxs[j][1].test(URL) ){
					t = regxs[j][0]
					r = document.getElementById(t)
					if(!r.firstChild.firstChild.firstChild.checked )
						break
					r.childNodes[1].textContent = parseInt(r.childNodes[1].textContent) + 1
					redirHelp(URL, t)
					visted[k] = true
					break
				}
			}
			
		}
	}
	while(p.firstChild)
		p.removeChild(p.firstChild)
}